package com.mmr.dxp.mapper;

import com.mmr.dxp.domain.DataTobePushed;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface DataTobePushedMapper {
    /**
     * 新增单条数据
     * @param data
     */
    @Insert("insert into " +
            " DATA_TOBE_PUSHED(`CLASS_NAME`,`METHOD_NAME`,`REQ_HEADERS`,`REQ_BODY`) values " +
            " (#{data.className},#{data.methodName},#{data.reqHeaders},#{data.reqBody}) ")
    void save(@Param("data") DataTobePushed data);

    /**
     * 批量新增数据
     * @param dataList
     */
    @Insert("<script>" +
            "insert into " +
            "   DATA_TOBE_PUSHED(`CLASS_NAME`,`METHOD_NAME`,`REQ_HEADERS`,`REQ_BODY`) values " +
            "   <foreach collection='dataList' item='data' index='index' separator=','> " +
            "       (#{data.className},#{data.methodName},#{data.reqHeaders},#{data.reqBody}) " +
            "   </foreach>" +
            "</script> ")
    void saveBatch(@Param("dataList")List<DataTobePushed> dataList);

    /**
     * 获取待发送数据
     * @return
     */
    @Select("select * from DATA_TOBE_PUSHED limit #{limit}")
    @Results(value={
        @Result(column="id", property="id", jdbcType= JdbcType.VARCHAR, id=true),
        @Result(column="CLASS_NAME", property="className",jdbcType= JdbcType.VARCHAR),
        @Result(column="METHOD_NAME", property="methodName", jdbcType= JdbcType.VARCHAR),
        @Result(column="REQ_HEADERS", property="reqHeaders", jdbcType= JdbcType.VARCHAR),
        @Result(column="REQ_BODY", property="reqBody", jdbcType= JdbcType.VARCHAR),
        @Result(column="UPDATE_TIME", property="updateTime", jdbcType= JdbcType.DATE)
    })
    List<DataTobePushed> listByLimit(@Param("limit")Integer limit);

    /**
     * 根据Id删除
     * @param id
     */
    @Delete("delete from DATA_TOBE_PUSHED where `id`=#{id} limit 1")
    void deleteById(@Param("id")Integer id);
    /**
     * 根据Id批量删除
     * @param idList
     */
    @Delete("<script>" +
            " delete from DATA_TOBE_PUSHED where `id` in " +
            "   <foreach collection='idList' item='id' open='(' separator=',' close=')'> " +
            "       #{id} " +
            "   </foreach>" +
            " </script> ")
    void deleteBatchById(@Param("idList")List<String> idList);
}